<?php

namespace Admin\Models\school;

use Admin\Models\BaseModel;
use Helpers\Cache;

class XhschoolInfo extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iSchoolId;

    /**
     *
     * @var string
     */
    public $sSchoolName;

    /**
     *
     * @var integer
     */
    public $iStatus;

    /**
     *
     * @var integer
     */
    public $iStage;

    /**
     *
     * @var integer
     */
    public $iRunLevel;

    /**
     *
     * @var string
     */
    public $sCode;

    /**
     *
     * @var string
     */
    public $sAbbr;

    /**
     *
     * @var integer
     */
    public $iType;

    /**
     *
     * @var integer
     */
    public $iApprovalStatus;

    /**
     *
     * @var integer
     */
    public $bWelfare;

    /**
     *
     * @var integer
     */
    public $iMainRunUserId;

    /**
     *
     * @var string
     */
    public $sRunUserId;

    /**
     *
     * @var string
     * @explain 考核方式，1=全校，2=年级
     */
    public $iExamineMode;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;


    public $sProvinceName;

    public $sCityName;

    public $sCountyName;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSchool_Info';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolInfo[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolInfo
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public function beforeSave()
    {
        if ($this->sRunUserId) {
            $this->sRunUserId = ',' . $this->sRunUserId . ',';
        }
        return true;
    }

    public function beforeCreate()
    {
        if (!$this->sCode) {
            //查找最大编号
            $maxCode = self::maximum(['column' => 'sCode']);
            if (!$maxCode) {
                $maxCode = 'XH00001';
            }
            $this->sCode = 'XH' . substr(strval(intval(substr($maxCode, 2, 5)) + 1 + 100000), 1, 5);
        }
        return parent::beforeCreate();
    }

    /**
     * 获取学校考核方式
     *
     * @param $schoolId
     * @return string
     *
     * create by ck 20180508
     */
    public static function getExamineMode($schoolId)
    {
        return self::findFirst([
            'columns'    => 'iExamineMode',
            'conditions' => 'iSchoolId = :schoolId:',
            'bind'       => ['schoolId' => $schoolId]
        ])->iExamineMode;
    }

    /**
     * 根据id数组删除相应记录，并返回结果
     *
     * @param $idArr
     * @return mixed
     *
     * create by ck 20160927
     */
//    public static function deleteRecordById($idArr)
//    {
//        return self::find(array(
//            'conditions' => 'iSchoolId IN ({idArr:array}) AND bDelete = 0',
//            'bind'       => ['idArr' => $idArr]
//        ))->delete();
//    }

    /**
     * 新建model，载入数据后返回
     *
     * @param array $data
     * @return XhschoolInfo
     *
     * create by ck 20160621
     * modify by ck 20160819    更改属性
     * modify by ck 20160927    结构化简
     */
    public static function createRecord($data)
    {
        $obj = $data['iSchoolId'] ? self::findFirst($data['iSchoolId']) : new self;
        $obj or $obj = new self();
        $obj->assign($data);
        return $obj;
    }

    /**
     * 从缓存中获取学校信息，按 学校id:学校名称 组合为数组
     * 缓存中不存在则读取并保存到缓存
     *
     * @return array|XhschoolInfo[]
     *
     * create by ck 20161107
     */
    public static function getSchoolArray()
    {
        $schoolList = Cache::get('school_list');
        if($schoolList) return $schoolList;
        $schoolList = self::find([
            'columns'    => 'iSchoolId,sSchoolName',
            'conditions' => 'bDelete=0'
        ])->toArray();
        $schoolList = array_column($schoolList, 'sSchoolName', 'iSchoolId');
        Cache::save('school_list', $schoolList);
        return $schoolList;
    }

    /**
     * 获取已上线学校列表
     *
     * @param $withName
     * @param $runOnly
     * @return array
     *
     * create by ck 20170314
     * modify by ck 20171228    添加是否返回名称参数
     * modify by dale 20180424  添加获取运营学校
     */
    public static function getSchoolSupportArray($withName = true,$runOnly = false)
    {
        if (!$runOnly){
            $schoolList = Cache::get('school_support');
        }else{
            $schoolList = Cache::get('school_run_list');
        }
        if($schoolList) return $withName ? $schoolList : array_keys($schoolList);
        $conditionArr = [
            'columns'    => 'iSchoolId,sSchoolName',
            'conditions' => 'bDelete = 0 AND (iStatus = 1 OR iStatus = 2)'
        ];
        if($runOnly){
            $conditionArr['conditions'] = 'bDelete = 0 AND iStatus = 2 ' ;
        }
        $schoolList = self::find($conditionArr)->toArray();
        $schoolList = array_column($schoolList, 'sSchoolName', 'iSchoolId');
        if($runOnly){
            Cache::save('school_run_list', $schoolList);
        }else{
            Cache::save('school_support', $schoolList);
        }
        return $withName ? $schoolList : array_keys($schoolList);
    }




    /**
     * 根据数组添加学校名称
     *
     * @param $arr
     * @param bool|true $isArray
     *
     * create by ck 20161108
     */
    public static function runSchoolName(&$arr, $isArray = true)
    {
        $schoolList = self::getSchoolArray();
        if ($isArray) {
            foreach ($arr as $key => $value) {
                $arr[$key]['sSchoolName'] = isset($schoolList[$value['iSchoolId']]) ? $schoolList[$value['iSchoolId']] : '未知';
            }
        } else {
            $arr['sSchoolName'] = isset($schoolList[$arr['iSchoolId']]) ? $schoolList[$arr['iSchoolId']] : '未知';
        }
    }

    /**
     * 删除学校缓存
     *
     * create by ck 20170707
     */
//    public static function delCache()
//    {
//        Cache::delete('school_list');
//        Cache::delete('school_support');
//    }

    /**
     * 根据学校id获取学校名称
     *
     * @param $schoolId
     * @return string
     *
     * create by sqb 20181226
     */
    public static function getSchoolNameById($schoolId)
    {
        $info = self::findFirst($schoolId);

        return $info->sSchoolName;
    }
}
